<template>
  <div class="app-container">
    <el-form
      :model="formData"
      ref="formData"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="时间" prop="voiceType">
        <el-date-picker
          v-model="dateRange"
          style="width: 100%"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="项目" prop="chatText">
        <el-select
          v-model="formData.productId"
          filterable
          placeholder="请选择"
          clearable
          @change="onChangeProduct"
        >
          <el-option
            v-for="item in productList"
            :key="item.id"
            :label="item.projectName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务" prop="chatText">
        <el-select
          v-model="formData.taskId"
          filterable
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in taskList"
            :key="item.id"
            :label="item.taskName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户状态" prop="chatText">
        <el-select
          v-model="formData.customerStatue"
          filterable
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in customerStatusList"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="质检状态" prop="chatText">
        <el-select
          v-model="formData.qualityStatus"
          filterable
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in qualityStatusList"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="chatText">
        <el-input
          v-model="formData.customerName"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="chatText">
        <el-input
          v-model="formData.customerTel"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="坐席工号">
        <el-input
          v-model="formData.workNumber"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通话时长" prop="chatText">
        <el-input
          v-model="formData.duration"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主叫号码">
        <el-select
          v-model="formData.callTel"
          filterable
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in callTelList"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          size="mini"
          @click="handleExport"
          >录音打包</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格   -->
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="主叫号码" align="center" prop="callTel" />
      <el-table-column
        label="客户号码"
        align="center"
        prop="customerTel"
      ></el-table-column>
      <el-table-column label="坐席姓名" align="center" prop="seatsName" />
      <el-table-column label="坐席工号" align="center" prop="seatsNumber" />
      <el-table-column label="开始时间" align="center" prop="beginTime" />
      <el-table-column label="通话时长" align="center" prop="duration" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="客户姓名" align="center" prop="customerName" />
      <el-table-column label="客户状态" align="center" prop="customerStatue" />
      <el-table-column label="录音地址" align="center" prop="tapeUrl" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            v-if="scope.row.ttsUrl"
            @click="onAudioDownload(scope.row)"
            type="text"
            >下载</el-button
          >
          <el-button
            size="mini"
            v-if="scope.row.ttsUrl"
            @click="onAudioDownload(scope.row)"
            type="text"
            >质检</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pagination.pageNum"
      :limit.sync="pagination.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { projectList } from "@/api/projectManagement/list";
import { taskList } from "@/api/taskManagement/index";
import { callDetailList } from "@/api/dataManagement/callDetails.js";
import { listData } from "@/api/system/dict/data";
export default {
  name: "ttsTestList",
  data() {
    return {
      loading: false,
      tableData: [], //表格数据
      showSearch: true, //显示搜索条件
      // 查询参数
      formData: {
        productId: "",
        taskId: "",
        customerStatue: "",
        qualityStatus: "",
        customerName: "",
        customerTel: "",
        workNumber: "",
        duration: "",
        callTel: "",
      },
      voiceTypeList: [], //音色数据
      pagination: {
        pageNum: 1,
        pageSize: 10,
      },
      customerStatusList: [
        { dictValue: "新车", dictLabel: "新车" },
        { dictValue: "旧车", dictLabel: "旧车" },
      ],
      qualityStatusList: [
        { dictValue: "未质检", dictLabel: "未质检" },
        { dictValue: "质检合格", dictLabel: "质检合格" },
        { dictValue: "质检不合格", dictLabel: "质检不合格" },
      ],
      callTelList:[ ],
      productList: [],
      taskList: [],
      dateRange: [], //日期范围
      total: 0,
      importShow: false, //导入
      addDialogShow: false,
      voiceTypeList: [],
      projectId: "",
      
    };
  },
  watch: {
    "formData.productId": {
      handler(val) {
        if (val) {
          this.projectId = val;
          this.getTaskList();
        } else {
          this.projectId = "";
        }
      },
      immediate: true,
    },
  },
  async created() {
    this.getListType()
    this.getTaskList();
    this.getProductList();
    await this.getList();
  },
  methods: {
    // 任务列表
    async getTaskList() {
      const params = {
        projectId: this.projectId,
        taskName: "",
        pageNum: 1,
        pageSize: 1000,
      };
      const { code, rows } = await taskList(params);
      if (code === 200) {
        this.taskList = rows;
      }
    },
    // 项目列表
    async getProductList() {
      const params = {
        status: "",
        projectName: "",
        pageNum: 1,
        pageSize: 1000,
      };
      const { code, rows } = await projectList(params);
      if (code === 200) {
        this.productList = rows;
      }
    },
    onChangeProduct(event) {
      this.projectId = event;
    },
    async getListType() {
      const { rows } = await listData({
        pageNum: 1,
        pageSize: 1000,
        dictType: "sssc_gateway_type",
      });
      this.callTelList = rows || [];
    },
    // 列表查询
    async getList() {
      let params = {
        ...this.formData,
        ...this.pagination,
      };
      this.loading = true;
      const { total, rows } = await callDetailList(
        this.addDateRange(params, this.dateRange)
      );
      this.total = total;
      this.tableData = rows;
      this.loading = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.pagination.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.formData = {
        productId: "",
        taskId: "",
        customerStatue: "",
        qualityStatus: "",
        customerName: "",
        customerTel: "",
        workNumber: "",
        duration: "",
        callTel: "",
      };
      this.dateRange = [];
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      const url = `/system/call/export`;
      const formData = JSON.parse(JSON.stringify(this.formData));
      let data = {
        ...formData,
        ...this.pagination,
      };
      if (this.dateRange?.length) {
        data.params =
          typeof data.params === "object" &&
          data.params !== null &&
          !Array.isArray(data.params)
            ? data.params
            : {};
        data.params["beginTime"] = this.dateRange[0];
        data.params["endTime"] = this.dateRange[1];
      }
      this.download(
        url,
        {
          ...data,
        },
        `callDetails_${new Date().getTime()}.xlsx`
      );
    },

    // 音频下载
    async onAudioDownload(row) {
      console.log(row, "row");
      const fileUrl = row?.ttsUrl;
      // 打开新标签页进行下载
      window.open(fileUrl, "_blank");
    },

    // 新建
    onClickAdd() {
      this.addDialogShow = true;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    // 取消
    onClose() {
      this.addDialogShow = false;
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped></style>
